package collections;

import java.util.HashSet;
import java.util.Set;

public class RemoveDups {

    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Usage RemoveDups args");
            System.exit(0);
        }
        String[] noDupsArry = removeDups(args);
        System.out.print("[");
        int index = 0;
        for (String item: noDupsArry) {
            index++;
            System.out.print(item);
            if (index < noDupsArry.length) {
                System.out.print(",");
            }
        }
        System.out.println("]");
        
        findDups(args);
    }

    public static String[] removeDups(String[] arry) {
        String[] temp = new String[arry.length];
        for (int i = 0; i < arry.length; i++) {
            for (int j = 0; j < temp.length; j++) {
                if (temp[j] == null) {
                    temp[j] = arry[i];
                    break;
                } else if (temp[j].equals(arry[i])) {
                    break;
                }
            }
        }
        //remove null value
        int count = 0;
        for (int i = 0; i < temp.length; i++) {
            if (temp[i] != null) {
                count++;
            } else {
                break;
            }
        }

        String[] newArry = new String[count];
        for (int i = 0; i < count; i++) {
            newArry[i] = temp[i];
        }
        
        return newArry;
    }
    
    public static void findDups(String[] args) {
        Set<String> s = new HashSet<String>();
        for (String a : args) {
            s.add(a);
        }
        System.out.println(s.size() + " distinct words: " + s);
    }
}
